//
//  LYQEditPositionViewController.m
//  ZCProject
//
//  Created by Zheng on 2016/12/25.
//  Copyright © 2016年 Zheng. All rights reserved.
//

#import "LYQEditPositionViewController.h"

#import "LYQEditPositionDefaultCell.h"
#import "LYQEditPositionHeaderView.h"
#import "LYQEditPositionTextCell.h"
#import "LYQEditPositionPickerView.h"

#import "LYQEditPositionModel.h"

@interface LYQEditPositionViewController () <UITableViewDelegate,UITableViewDataSource,LYQEditPositionPickerViewDelegate>

@property (nonatomic, strong) UIBarButtonItem *leftItem; //取消
@property (nonatomic, strong) UIBarButtonItem *rightItem; //发布
@property (nonatomic, strong) LYQEditPositionPickerView *pickerView; //选择视图 (学历,薪资等)

@property (nonatomic, strong) LYQEditPositionHeaderView *titleHeader; //职位描述

@property (nonatomic, strong) LYQEditPositionModel *model;

@end

@implementation LYQEditPositionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"编辑职位";
    
    _model = [[LYQEditPositionModel alloc] init];
    //TODO: 默认获取注册人公司
    _model.commanyName = @"博信 (北京) 会展有限责任公司";
    
    self.listTableView.delegate = self;
    self.listTableView.dataSource = self;
    self.listTableView.estimatedRowHeight = 100;
    self.listTableView.rowHeight = UITableViewAutomaticDimension;
    [self.listTableView registerClass:[LYQEditPositionDefaultCell class] forCellReuseIdentifier:kLYQEditPositionDefaultCell_ID1];
    [self.listTableView registerClass:[LYQEditPositionDefaultCell class] forCellReuseIdentifier:kLYQEditPositionDefaultCell_ID2];
    [self.listTableView registerClass:[LYQEditPositionDefaultCell class] forCellReuseIdentifier:kLYQEditPositionDefaultCell_ID3];
    [self.listTableView registerNib:[UINib nibWithNibName:@"LYQEditPositionTextCell" bundle:nil] forCellReuseIdentifier:kLYQEditPositionTextCell_ID];
    
    self.navigationItem.leftBarButtonItem = self.leftItem;
    self.navigationItem.rightBarButtonItem = self.rightItem;
    
    [self.view addSubview:self.pickerView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [self setNavigationBarStyleWithTintColor:kLYQBlue withTitleColor:[UIColor whiteColor] withBackState:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self setNavigationBarStyleWithTintColor:[UIColor whiteColor] withTitleColor:kLYQBlack withBackState:YES];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    
    self.navigationItem.hidesBackButton = NO;
}

- (void)setNavigationBarStyleWithTintColor:(UIColor *)tintColor withTitleColor:(UIColor *)titleColor withBackState:(BOOL)state {
    
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = tintColor;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:titleColor}];
    self.navigationItem.hidesBackButton = state;
}

#pragma mark - lazy load subviews
- (UIBarButtonItem *)leftItem {
    if (_leftItem) {
        return _leftItem;
    }
    
    _leftItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(leftItemAction:)];
    [_leftItem setTitleTextAttributes:@{NSFontAttributeName:kWFont(16)} forState:UIControlStateNormal];
    
    return _leftItem;
}

- (UIBarButtonItem *)rightItem {
    if (_rightItem) {
        return _rightItem;
    }
    
    _rightItem = [[UIBarButtonItem alloc] initWithTitle:@"发布" style:UIBarButtonItemStylePlain target:self action:@selector(rightItemAction:)];
    [_rightItem setTitleTextAttributes:@{NSFontAttributeName:kWFont(16)} forState:UIControlStateNormal];
    
    return _rightItem;
}

- (LYQEditPositionPickerView *)pickerView {
    if (_pickerView) {
        return _pickerView;
    }
    
    _pickerView = [[LYQEditPositionPickerView alloc] initWithFrame:self.view.bounds];
    _pickerView.delegate = self;
    _pickerView.model = _model;
    
    return _pickerView;
}

#pragma mark - 点击事件
#pragma mark 取消事件 返回上一级页面
- (void)leftItemAction:(UIBarButtonItem *)item {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark 发布事件 发布职位
- (void)rightItemAction:(UIBarButtonItem *)item {
    NSLog(@"---%@",_model.education);
    NSLog(@"---%@",_model.experience);
    NSLog(@"---%@",_model.salary);
    NSLog(@"---%@",_model.jobName);
    NSLog(@"---%@",_model.jobDescription);
    NSLog(@"---%@",_model.commanyName);
    
    if ([_model.commanyName isEqualToString:@""]) {
        [[TKAlertCenter defaultCenter] postAlertWithMessage:@"请填写公司名称"];
        return;
    }

    if ([_model.jobName isEqualToString:@""]) {
        [[TKAlertCenter defaultCenter] postAlertWithMessage:@"请填写职务名称"];
        return;
    }
    
    if (_model.jobName.length > 8 || _model.jobName.length < 2) {
        [[TKAlertCenter defaultCenter] postAlertWithMessage:@"请填写2-8位长度的职务名称"];
        return;
    }
    
    if ([_model.education isEqualToString:@""]) {
        [[TKAlertCenter defaultCenter] postAlertWithMessage:@"请填写学历要求"];
        return;
    }
    
    if ([_model.experience isEqualToString:@""]) {
        [[TKAlertCenter defaultCenter] postAlertWithMessage:@"请填写经验要求"];
        return;
    }
    
    if ([_model.salary isEqualToString:@""]) {
        [[TKAlertCenter defaultCenter] postAlertWithMessage:@"请填写薪资范围"];
        return;
    }
    
    if (_model.city) {
        //TODO:工作城市
    }
    
    if ([_model.jobDescription isEqualToString:@""]) {
        [[TKAlertCenter defaultCenter] postAlertWithMessage:@"请填写职位描述"];
        return;
    }


    
    
}

#pragma mark - tableview datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 6;
    } else {
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        NSString *identifier = @"";
        
        if (indexPath.row == 0) {
            identifier = kLYQEditPositionDefaultCell_ID1;
        } else if (indexPath.row == 1) {
            identifier = kLYQEditPositionDefaultCell_ID2;
        } else {
            identifier = kLYQEditPositionDefaultCell_ID3;
        }
        
        LYQEditPositionDefaultCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
        
        cell.model = _model;
        cell.titleLbl.text = _model.titlesArr[indexPath.row];
        
        if (indexPath.row == 2) {
            cell.valueLbl.text = _model.education;
        } else if (indexPath.row == 3) {
            cell.valueLbl.text = _model.experience;
        } else if (indexPath.row == 4) {
            cell.valueLbl.text = _model.salary;
        }
        
        return cell;
    } else {
        LYQEditPositionTextCell *cell = [tableView dequeueReusableCellWithIdentifier:kLYQEditPositionTextCell_ID forIndexPath:indexPath];
        cell.model = _model;
        
        return cell;
    }
}

#pragma mark - tableview delegate
#pragma mark 头视图高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return kWScale(10);
    } else {
        return kWScale(50);
    }
}

#pragma mark 尾视图高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.0001;
}

#pragma mark 自定义头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        return self.titleHeader;
    } else {
        return nil;
    }
}

- (LYQEditPositionHeaderView *)titleHeader {
    if (_titleHeader) {
        return _titleHeader;
    }
    
    _titleHeader = [[LYQEditPositionHeaderView alloc] init];
    
    return _titleHeader;
}


#pragma mark 设置分割线偏移量
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
}

#pragma mark 单元格点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [self.listTableView endEditing:YES];
    
    if (indexPath.row == 2) {
        [_pickerView showStateActionWithStyle:LYQEditPositionPickerViewStyleEducation];
    } else if (indexPath.row == 3) {
        [_pickerView showStateActionWithStyle:LYQEditPositionPickerViewStyleExperience];
    } else if (indexPath.row == 4) {
        [_pickerView showStateActionWithStyle:LYQEditPositionPickerViewStyleSalary];
    }

}


//- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    
//    [self.view endEditing:YES];
//    
//    [self.listTableView mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(kWScale(0));
//    }];
//    
//}

#pragma mark - 自定义代理方法
#pragma mark LYQEditPositionDefaultCellDelegate
- (void)updateUIWithStyle:(LYQEditPositionPickerViewStyle)style {
    
    if (style == LYQEditPositionPickerViewStyleEducation) {
        [self.listTableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:2 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
    } else if (style == LYQEditPositionPickerViewStyleExperience) {
        [self.listTableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:3 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
    } else if (style == LYQEditPositionPickerViewStyleSalary) {
        [self.listTableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:4 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
    }
}


#pragma mark - 内存管理
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
